//用户相关的仓库
import { defineStore } from "pinia";
import { reqWebInfo, reqSortInfo } from "@/api/webInfo";
const useWebInfoStore = defineStore('WebInfo', {
    state: () => {
        return {
            webInfo: JSON.parse(localStorage.getItem("webInfo") || '{"webName": "", "webTitle": [], "notices": [], "footer": "", "backgroundImage": "", "avatar": ""}'),
            sortInfo: JSON.parse(localStorage.getItem("sortInfo") || '[]'),
            toolbar: JSON.parse(localStorage.getItem("toolbar") || '{"visible": false, "enter": true}'),
            
        }
    },
    actions: {
        changeToolbarStatus(toolbarState: any) {
            this.toolbar = toolbarState;
            localStorage.setItem("toolbar", JSON.stringify(toolbarState));
        },
       
        async loadWebInfo() {
            let result: any = await reqWebInfo()
            if (result.code == 200) {
                this.webInfo.webTitle = result.data.webTitle.split('');
                this.webInfo.notices = result.data.notices
                this.webInfo = result.data;
                localStorage.setItem("webInfo", JSON.stringify(result.data));
            } else {
                return Promise.reject(new Error(result.message))
            }
        },
        async loadSortInfo() {
            let result: any = await reqSortInfo()
            if (result.code == 200) {
                this.sortInfo = this.sortInfo.sort((s1: any, s2: any) => s1.priority - s2.priority);
                localStorage.setItem("sortInfo", JSON.stringify(result.data.sort((s1: any, s2: any) => s1.priority - s2.priority)));
            }
        },
       
    },
    getters: {

    }
})

export default useWebInfoStore;